// pages/showCases/showCases.js
var app = getApp()

var data_cases = require("../../data/data_cases.js")
var util = require("../../utils/util.js")
var getCase_url = 'https://www.hkzhaoscut.top/doctor/getCaseList'
var pageNum = 2
var pageSize = 5
var degreeSelection = ['无症状', '轻症', '重症', '治愈', '死亡']

// 获取初始的病例数据
var getInitCases = function(that,token) {
  var doctorToken = token
  // console.log(doctorToken)
  wx.request({
    url: getCase_url,
    header: {
      'Authorization': doctorToken
    },
    data: {
      pageNum: 1,
      pageSize: pageSize
    },
    success: res => {
      console.log('获取初始数据成功', res.data)
      if (res.data.code == 200) {
        console.log('获取初始数据成功', res.data)
        var list = res.data.data.list
        var tmplist = []
        if (list.length != 0) {
          list.forEach(function(item, index) {
            tmplist[index] = {}
            var date = new Date(item.createTime)
            tmplist[index].reportDate = util.formatTimeWithoutHour(date)
            tmplist[index].illDate = util.formatTimeWithoutHour(date)
            tmplist[index].pSex = item.gender
            tmplist[index].patientName = item.infectname
            tmplist[index].pAddress = util.formatAddress(item.locate) 
            tmplist[index].pPhone = item.telephone
            tmplist[index].condition = item.degree
            tmplist[index].conditionId = degreeSelection.indexOf(item.degree)
            tmplist[index].illness = item.diseasetype
            tmplist[index].pAge = item.age
            tmplist[index].remarks = item.note
            tmplist[index].doctortokenid = item.doctortokenid
            tmplist[index].caseid = item.caseid
          })
          var curCases = tmplist
          that.setData({
            case_list: curCases
          })
        }

      }

    }
  })

}
// 获取更多的病例数据
var getMoreCases = function (that) {
  var doctorToken = that.data.doctorToken
  wx.request({
    url: getCase_url,
    header: {
      'Authorization': doctorToken
    },
    data: {
      pageNum: pageNum,
      pageSize: pageSize
    },
    success: res => {
      if (res.data.code == 200) {
        console.log('获取初始数据成功', res.data)
        var list = res.data.data.list
        var tmplist = []
        if (list.length != 0) {
          pageNum++
          list.forEach(function (item, index) {
            tmplist[index] = {}
            var date = new Date(item.createTime)
            tmplist[index].reportDate = util.formatTimeWithoutHour(date)
            tmplist[index].illDate = util.formatTimeWithoutHour(date)
            tmplist[index].pSex = item.gender
            tmplist[index].patientName = item.infectname
            tmplist[index].pAddress = util.formatAddress(item.locate)
            tmplist[index].pPhone = item.telephone
            tmplist[index].condition = item.degree
            tmplist[index].conditionId = degreeSelection.indexOf(item.degree)
            tmplist[index].illness = item.diseasetype
            tmplist[index].pAge = item.age
            tmplist[index].remarks = item.note
            tmplist[index].doctortokenid = item.doctortokenid
            tmplist[index].caseid = item.caseid
          })
          var originCases = that.data.case_list
          var curCases =originCases.concat(tmplist)
          that.setData({
            case_list: curCases
          })
        }

      }

    }
  })

}
Page({
  data: {
    case_list: [], //案例数组
    timeSelection: ['最晚上报', '最早上报'],
    timeSelected: true, //是否点击了按上报时间排序
    byTime: 0, //0代表按最晚上报排序 1代表按最早上报排序
    maskFlag: true, //遮罩层flag
    degreeSelection: ['无症状', '轻症', '重症', '治愈', '死亡'],
    degreeSelected: true, //是否点击的按紧急程度排序的按钮
    byDegree: 0, //0-4分表表示不同程度
    doctorToken:app.globalData.doctorToken
  },

  // 跳转修改画面
  toModify(e) {
    var index = e.currentTarget.dataset.index
    var case_data = JSON.stringify(this.data.case_list[index])
    wx.navigateTo({
      url: '../modifyCases/modifyCases?case_data=' + case_data,
    })
  },


  // 显示按上报时间排序的顶部栏
  forTime(e) {
    var timeSelected = !this.data.timeSelected
    this.setData({
      timeSelected: timeSelected
    })
    this.showMask()
  },
  // 显示按状态排序的顶部栏
  forDegree(e) {
    var degreeSelected = !this.data.degreeSelected
    this.setData({
      degreeSelected: degreeSelected
    })
    this.showMask()
  },
  // 状态的选择项
  selectDegree(e) {
    var that = this
    // console.log(e)
    var selected = e.currentTarget.id
    var condition = that.data.degreeSelection[selected]
    that.setData({
      byDegree: selected
    })
    // console.log(condition)
    var list = that.data.case_list.sort(that.compareDegree('condition', condition));
    that.setData({
      case_list: list
    })
    that.forDegree()
  },
  // 比较状态
  compareDegree: function(property, condition) {
    return function(a, b) {
      if (a[property] == condition) {
        var value1 = 1
      } else {
        var value1 = 0
      }
      if (b[property] == condition) {
        var value2 = 1
      } else {
        var value2 = 0
      }
      // console.log(value1,value2)
      return value2 - value1;

    }
  },
  // 上报时间的选择项
  selectTime(e) {
    // console.log(e)
    var that = this
    var selected = e.currentTarget.id
    that.setData({
      byTime: selected
    })
    var list = that.data.case_list.sort(that.compareTime('reportDate', selected));
    that.setData({
      case_list: list
    })
    that.forTime()
    // console.log(list)
  },
  // 比较日期
  compareTime: function(property, way) {
    return function(a, b) {
      var value1 = Date.parse(a[property]);
      var value2 = Date.parse(b[property]);
      if (way == 0) {
        return value2 - value1;
      } else {
        return value1 - value2;
      }
    }
  },
  // 展示遮罩层
  showMask() {
    var maskFlag = !this.data.maskFlag
    this.setData({
      maskFlag: maskFlag
    })
  },
  //触底函数
  onReachBottom: function() {
    console.log("到底部了")
    getMoreCases(this)
  },

  // onLoad函数
  onLoad(e) {
    var userInfo = wx.getStorageSync('userInfo')
    // console.log(userInfo)
    if (app.globalData.userType == 'doctor' && app.globalData.loginStauts){
      getInitCases(this, userInfo.doctorToken)
    }
    this.setData({
      doctorToken: userInfo.doctorToken
    })
    
  
  },
  // onShow函数
  onShow(e) {
    // 这里向后台请求数据

  }
})